//树节点信息
var opernode = null;
var treeObj = null;

var messageId = null;
var menuId = null;

var menuCurLocation = null;//当前菜单显示位置信息
var menuCurTitle = null;//当前菜单显示标题
var menuCurDescription = null;//当前菜单显示描述

//菜单目录树形结构配置
var treeSetting = {
		view: {
			selectedMulti: false,
			dblClickExpand: false
		},
		check: {
			enable: false
		},
		edit: {
			enable: false,
			showRemoveBtn: false,
			showRenameBtn: false
		},
		data: {
			keep: {
				parent:false,
				leaf:false
			},
			 key:  {
		           name: "MENU_NAME",
			       title: "MENU_NAME"
			},
		     simpleData: {
				  enable: true,
				  idKey: "MENU_ID",
				  pIdKey: "PARENT_ID",
				  rootPId: ""
		    }
		},
		callback: {		
			onClick: TreeOnClick//,
			//onRightClick: OnRightClick
		}
	};

/*
 * {文档加载完成执行}
 */
$(document).ready(function(){
	initMenuInfoTree();
	hideAllForm();
});



function queryContent()
{
	var column = [];
	column =  [
	 {field:'CONTENT_ID',hidden:true},
     {field:'MESSAGE_ID',checkbox:true},
     {field:'CONTENT_TITLE',title:'标题',width:"250"},
     {field:'CONTENT_DESC',title:'描述',width:"250"},
     {field:'CONTENT_URL',title:'路径',width:"200"}
     ];
	using('datagrid', function(){

		$('#contentGrid').datagrid({  
			width:'auto',
			height:'auto',
			nowrap: false,
			striped: true,
			pagination:true,
			pageNumber : 1,
			columns:[column],
		    url:appPath+'/content/listContent?messageId='+messageId,
		    toolbar:[{
		       	 text:'<span class="glyphicon glyphicon-save"></span> 保存',
		       	 handler:saveMessage
		        }],
		    onLoadSuccess : function(data){
		    	
		    	$('#contentGrid').datagrid("uncheckAll");
		    	
		    	for ( var i = 0; i < data.total; i++ ){
		    		if ( data.rows[i].MESSAGE_ID == messageId ){
		    			$('#contentGrid').datagrid("checkRow",i);
		    		}
		    	}
		    	
		    }
		}); 
	});
}

function saveMessage(){
	
	using('datagrid', function(){
		var rows = $('#contentGrid').datagrid("getChecked");
		
		var data = {"MESSAGE_ID":messageId,"MENU_ID":menuId,"CONTENTS":rows};
		$.ajax({
		   type: "POST",
		   async: false,
		   url: appPath+"/message/saveMessage",
//		   dataType:"json",
		   data : {"data":$.toJSONString(data)},
	       success: function(data){
	    	   queryContent();
	    	   $("#alert").html("保存成功！");
		   },
		   error:function(xhr,textStatus,errorThrown){
			   $("#alert").html(textStatus);
		       
		   }
		});
		
		
	});
	
	
}


/**
 * 初始化左侧树控件
 */
function initMenuInfoTree(){
	$.ajax({
	   type: "POST",
	   async: false,//同步发送请求数据
	   url: appPath+"/menu?method=getTreeData",
	   data : {},
	   beforeSend : function(){
//		   $.showProgress();
	   },
	   complete : function(){
//		   $.hideProgress();
	   },
	   timeout:120*1000,
	   dataType:"json",
       success: function(data){
    	   if(data){
    		   var queryTreeNodeData = data;
	   		   if(queryTreeNodeData.length==0){
	   			   $.messageBox.alert({title : '错误',content : '查不到数据!'});
	   		   }
	   		   if(queryTreeNodeData.length>0){//初始化左边树控件
	   		       $.fn.zTree.init($("#menuTree"), treeSetting,queryTreeNodeData); 
	   		       treeObj = $.fn.zTree.getZTreeObj("menuTree");	//参数为树id		       
	   		   }   
    	   }else{
    		   $.messageBox.alert({title : '异常',content : '获取菜单信息异常!'}); 
    	   }
	   },
	   error:function(xhr,textStatus,errorThrown){
		   $.hideprogress();
	       $.messageBox.alert({title : '错误',content : textStatus});
	   }
	});
    
}

function hideAllForm(){
	$("#gridPanel").hide();
}

/*
 * 处理左键单击节点事件
 */
function TreeOnClick(event, treeId, treeNode) {
	opernode = treeNode;
    var nodeData = treeNode;//单击的节点
	var form = null; 
    if( opernode.level == 2 ){//处理中间节点和叶子节点  
    	menuId = nodeData.MENU_ID;
    	$.ajax({
    		   type: "POST",
    		   async: false,//同步发送请求数据
    		   url: appPath+"/message/getMessageId",
    		   data : {"menuId":menuId},
    		   timeout:120*1000,
    	       success: function(data){
    	    	   messageId = data;
    	    	   $('#gridPanel').show();
    	    	   queryContent();
    		   }
    		});
	    
  	}else{
  		hideAllForm();
  	}
}
 


